package demo;

import com.sun.javafx.application.PlatformImpl;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;

public class XBrowser extends JPanel {
    private static final String RUN_JAVASCRIPT = "Run JavaScript...";
    private static final String CLOSE_JAVASCRIPT = "Close JavaScript Console";
    private static String DEFAULT_URL = "http://www.oschina.net";
    private JButton backwardButton;
    private JButton forwardButton;
    private JButton refreshButton;
    private JButton stopButton;
    private JMenuItem consoleMenuItem;
    private WebView view;
    JFXPanel jfxPanel;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    public XBrowser() {
        PlatformImpl.startup(new Runnable() {
            @Override
            public void run() {
                jfxPanel = new JFXPanel();
                view = new WebView();
                final StackPane layout = new StackPane();
                Scene scene = new Scene(layout);
                layout.getChildren().addAll(view);
                jfxPanel.setScene(scene);
                jfxPanel.setVerifyInputWhenFocusTarget(true);
            }
        });
        this.backwardButton = createBackwardButton(view);
        this.forwardButton = createForwardButton(view);
        this.refreshButton = createRefreshButton(view);
        this.stopButton = createStopButton(view);

        setLayout(new BorderLayout());
        JToolBar toolbar = new JToolBar();
        //add(toolbar, BorderLayout.NORTH);
        toolbar.setBorderPainted(false);
        toolbar.setFloatable(false);
        toolbar.add(backwardButton);
        toolbar.add(refreshButton);
        toolbar.add(forwardButton);
        toolbar.addSeparator();
        add(jfxPanel);
    }

    private static JButton createBackwardButton(final WebView view) {
        return createButton("back", new AbstractAction() {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
                PlatformImpl.startup(new Runnable() {
                    @Override
                    public void run() {
                        int currentIndex = view.getEngine().getHistory().getCurrentIndex();
                        if (currentIndex > 0) {
                            view.getEngine().getHistory().go(currentIndex - 1);
                        }
                    }
                });
            }
        });
    }

    private static JButton createForwardButton(final WebView view) {
        return createButton("forward", new AbstractAction() {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
                int currentIndex = view.getEngine().getHistory().getCurrentIndex();
            }
        });
    }

    private static JButton createRefreshButton(final WebView view) {
        return createButton("refresh", new AbstractAction() {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
                PlatformImpl.startup(new Runnable() {
                    @Override
                    public void run() {
                        view.getEngine().reload();
                    }
                });
            }
        });
    }

    private static JButton createStopButton(final WebView view) {
        return createButton("stop", new AbstractAction() {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
            }
        });
    }

    private static JButton createButton(String paramString, Action paramAction) {
        ActionButton localActionButton = new ActionButton(paramString, paramAction);
        String str = paramString.toLowerCase();
        return localActionButton;
    }

    public static ImageIcon getIcon(String paramString) {
        return new ImageIcon(XBrowser.class.getResource("/" + paramString));
    }

    private static class ActionButton extends JButton {
        private ActionButton(String paramString, Action paramAction) {
            super();
            setContentAreaFilled(false);
            setBorder(BorderFactory.createEmptyBorder());
            setBorderPainted(false);
            setRolloverEnabled(true);
            setToolTipText(paramString);
            setText(null);
            setFocusable(false);
            setDefaultCapable(false);
        }
    }
    public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
        showXBrowser("http://163.com");
    }

    public static void showXBrowser(String url) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        initEnvironment();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame localJFrame = new JFrame("趋恒科技IDE插件");
                localJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                localJFrame.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent paramAnonymousWindowEvent) {
                        System.exit(0);
                    }
                });
                XBrowser xb = new XBrowser();
                PlatformImpl.startup(new Runnable() {
                    @Override
                    public void run() {
                        xb.view.getEngine().load(url);
                    }
                });
                localJFrame.add(xb);
                localJFrame.setSize(1024, 768);
                localJFrame.setLocationRelativeTo(null);
///                localJFrame.setIconImage(getIcon("jxbrowser16x16.png").getImage());
                localJFrame.setVisible(true);
            }
        });
    }

    private static void initEnvironment() throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException

    {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("jxbrowser.ie.compatibility-disabled", "true");
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    }
}